# -*- coding: utf-8 -*-

import time
from odoo import api, models, _
from odoo.exceptions import UserError

class ReportPartnerLedger(models.AbstractModel):
    _inherit = 'report.accounting_pdf_reports.report_partnerledger'

    # 业务伙伴 应收应付明细账
    # 余额的计算为查询期间的余额累计, 起初值是没有的
    # 设定查询期间为一个自然月
    # 需要做这样一个处理:
    # 月底做 一个科目余额的 凭证.== 类同于资产负债表
    # 这样所有的科目,余额全部结转为0. 
    # 科目余额凭证的摘要为, 结转到下月
    # 月初 做一个起初余额凭证, 是上月科目余额凭证的反向操作
    # 起初余额凭证的摘要为 结转自上月
    # 这样处理后, 只要是按照自然月进行查询, 余额栏就和中国会计习惯的数据一致了

    @api.model
    def _get_report_values2(self, docids, data=None):
        res = self._get_report_values( docids, data)

        partners = []
        fn_sum = res.get('sum_partner')
        fn_lines= res.get('lines')
        for ptn in res['docs']:
            partner_data = {}
            partner_data['id'] = ptn.id
            partner_data['partner_id'] = ptn.id
            partner_data['partner_name'] = ptn.name
            partner_data['partner_display_name'] = ptn.display_name
            partner_data['debit'] = fn_sum(res['data'], ptn, 'debit')
            partner_data['credit'] = fn_sum(res['data'], ptn, 'credit')
            partner_data['balance'] = fn_sum(res['data'], ptn, 'debit - credit')
            partner_data['lines'] = fn_lines(res['data'], ptn )
            partners = partners + [partner_data]

        res['report_data'] = partners

        return res


